package com.cqggrw.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import com.cqggrw.dao.AdDaoI;
import com.cqggrw.dao.impl.AdDaoImpl;
import com.cqggrw.domain.Ad;
import com.cqggrw.service.AdServiceI;
import com.cqggrw.util.Utils;

public class AdServiceImpl implements AdServiceI {

	private AdDaoI dao = new AdDaoImpl();

	public List<Ad> get() {
		return dao.get();
	}

	public boolean modify(Ad ad, String realPath) {
		boolean flag = false;
		if (ad.getImg() != null) {
			ad.setImgName(UUID.randomUUID().toString() + "." + ad.getImgFileName().replace(".", ",").split(",")[1]);
			String savePath = realPath + Utils.separator + "images" + Utils.separator + "AdImg" + Utils.separator;
			try {
				Utils.log(savePath);

				if (Utils.upFile(ad.getImgName(), savePath, ad.getImg())) {
					flag = true;
				}
			} catch (IOException e) {
				Utils.log(e);
			}
		}
		if(dao.modify(ad)) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}

	public Ad get(int id) {
		return dao.get(id);
	}

}
